package com.ruoyi.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.annotation.HasPermi;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.demo.domain.dao.DemoUser;
import com.ruoyi.demo.domain.persist.DemoUserPo;
import com.ruoyi.demo.domain.query.DemoUserQo;
import com.ruoyi.demo.domain.view.DemoUserVo;
import com.ruoyi.demo.domain.wrapper.query.DemoUserWrapperQ;
import com.ruoyi.demo.domain.wrapper.view.DemoUserWrapperV;
import com.ruoyi.demo.service.IDemoUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 人员Controller
 *
 * @author 尹国春
 * @date 2022-02-25
 */
@Api(value = "人员", tags = {"人员"})
@RestController
@RequestMapping("/demo/user")
public class DemoUserController extends BaseController
{
    @Autowired
    private IDemoUserService demoUserService;

    /**
     * 人员分页查询
     */
    @ApiOperation(value="人员分页查询",notes="分页参数：pageNum,pageSize,orderByColumn(数据库字段名称),isAsc(asc|desc)")
    @HasPermi("demo:user:query")
    @PostMapping("/list")
    public TableDataInfo<DemoUserVo> list(@RequestBody DemoUserQo demoUser)
    {
        IPage<DemoUser> page=demoUserService.page(demoUser.getPage(),DemoUserWrapperQ.getQueryWrapper(demoUser));
        return DemoUserWrapperV.build().pageVO(page);
    }

    /**
     * 人员导出
     */
    @ApiOperation("人员导出")
    @HasPermi("demo:user:query")
    @Log(title = "人员导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DemoUserQo demoUser)
    {
        List<DemoUser> list = demoUserService.list(DemoUserWrapperQ.getQueryWrapper(demoUser));
        List<DemoUserVo> voList = DemoUserWrapperV.build().listVO(list);
        ExcelUtil<DemoUserVo> util = new ExcelUtil<>(DemoUserVo.class);
        util.exportExcel(response, voList, "人员数据");
    }

    /**
     * 获取人员详细信息
     */
    @ApiOperation("人员信息")
    @HasPermi("demo:user:query")
    @GetMapping(value = "/get/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") String userId)
    {
        DemoUser entity=demoUserService.getById(userId);
        return AjaxResult.success(DemoUserWrapperV.build().entityVO(entity));
    }

    /**
     * 新增人员
     */
    @ApiOperation("人员新增")
    @HasPermi("demo:user:add")
    @Log(title = "新增人员", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody DemoUserPo demoUser)
    {
        return toAjax(demoUserService.insert(demoUser));
    }

    /**
     * 修改人员
     */
    @ApiOperation("人员修改")
    @HasPermi("demo:user:edit")
    @Log(title = "修改人员", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody DemoUserPo demoUser)
    {
        return toAjax(demoUserService.updateById(demoUser));
    }

    /**
     * 删除人员
     */
    @ApiOperation("人员删除")
    @HasPermi("demo:user:remove")
    @Log(title = "删除人员", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{userIds}")
    public AjaxResult remove(@PathVariable String[] userIds)
    {
        return toAjax(demoUserService.removeByIds(Arrays.asList(userIds)));
    }
}
